package com.example.himalayaproject.subscription.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.os.ResultReceiver;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.himalayaproject.R;
import com.example.himalayaproject.subscription.adapter.FragmentSepciaDetailsAdapter;
import com.example.himalayaproject.subscription.bean.detailsentity.DetailsEntity;
import com.example.himalayaproject.subscription.callback.HttpSercive;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class SpecialDetailsFragment extends Fragment implements Callback<DetailsEntity> {
    private RecyclerView recyclerView;
    private FragmentSepciaDetailsAdapter adapter;

    public static SpecialDetailsFragment newInstance(String albumId) {
        Bundle args = new Bundle();
        args.putString("albumId", albumId);
        SpecialDetailsFragment fragment = new SpecialDetailsFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_special_details, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        aboutRecycler(view);
    }

    private void aboutRecycler(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_spcial_details_recycler);
        String albumId = getArguments().getString("albumId");
        HttpSercive sercive = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpSercive.class);
        sercive.getDetail(albumId).enqueue(this);
    }

    @Override
    public void onResponse(Call<DetailsEntity> call, Response<DetailsEntity> response) {
        adapter = new FragmentSepciaDetailsAdapter(getContext());
        adapter.addEntity(response.body().getData());
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFailure(Call<DetailsEntity> call, Throwable t) {

    }
}
